<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>员工的编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

      <style>
          .layui-form-item .layui-input-inline{
              width: 300px;
          }
      </style>
  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
          <input type="hidden" name="emplId" th:value="${param.id}">
          <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>员工的编码
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="emplCode" readonly lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
            </div>
          <div class="layui-form-item">
              <label class="layui-form-label">
                  <span class="x-red">*</span>姓名
              </label>
              <div class="layui-input-inline">
                  <input type="text" name="emplName" required="" lay-verify="required"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">

              </div>
          </div>
          <div class="layui-form-item">
              <label for="phone" class="layui-form-label">
                  <span class="x-red">*</span>手机
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="phone" name="emplPhone" required="" lay-verify="phone"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">

              </div>
          </div>
          <div class="layui-form-item">
              <label  class="layui-form-label">
                  <span class="x-red">*</span>性别
              </label>
              <div class="layui-input-inline">
                  <input type="radio" name="emplGender" value="1" title="男人">
                  <input type="radio" name="emplGender" value="2" title="女人" >
                  <input type="radio" name="emplGender" value="3" title="保密" >
              </div>
              <div class="layui-form-mid layui-word-aux">

              </div>
          </div>
          <div class="layui-form-item">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>状态
                </label>
                <div class="layui-input-inline">
                    <input type="radio" name="emplStatus" value="1" title="正常">
                    <input type="radio" name="emplStatus" value="2" title="禁用" >
                    <input type="radio" name="emplStatus" value="3" title="离职" >
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
          </div>
          <div class="layui-form-item">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>部门
                </label>
                <div class="layui-input-inline">
                    <select name="deptId" lay-verify="required">
                        <option value="0">北京</option>
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
            </div>
          <div class="layui-form-item">
              <label class="layui-form-label"><span class="x-red">*</span>角色</label>
              <div class="layui-input-block" id="role-container">
                <input type="checkbox" name="roleIds" lay-skin="primary" title="超级管理员" checked="">
                <input type="checkbox" name="roleIds" lay-skin="primary" title="编辑人员">
                <input type="checkbox" name="roleIds" lay-skin="primary" title="宣传人员" checked="">
              </div>
          </div>
          <div class="layui-form-item">
              <label class="layui-form-label">
                  <span class="x-red">*</span>备注
              </label>
              <div class="layui-input-inline">
                  <textarea name="emplRemark" placeholder="请输入备注信息" class="layui-textarea"></textarea>
              </div>
          </div>
          <div class="layui-form-item">
              <label class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  编辑
              </button>
          </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
        
          //自定义验证规则
          form.verify({
            nikename: function(value){
              if(value.length < 5){
                return '昵称至少得5个字符啊';
              }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#L_pass').val()!=$('#L_repass').val()){
                    return '两次密码不一致';
                }
            }
          });

          //监听提交
          form.on('submit(add)', function(data){
            var param = data.field;
            var roleIds = [];
            // 需要单独的获取角色ID的数组
            $("div[class='layui-unselect layui-form-checkbox layui-form-checked']").each(function () {
                var input = $(this).prev();
                roleIds.push(input.val());
            });
            // 添加到param中
            param.roleIds = roleIds;
            $.ajax({
              type: "post",// http请求方法
              url:"/empl/edit/do",// 后端的接口
              data: param,// 传递到后端的数据
              dataType: "json", // 后端返回的数据类型
              success: function (res) {// 服务器成功响应之后对应的逻辑
                  if (res.code === 200){
                      layer.alert(res.msg, {icon: 6},function () {
                          // 重载表格
                          parent.tableIns.reload();
                          // 获得frame索引
                          var index = parent.layer.getFrameIndex(window.name);
                          //关闭当前frame
                          parent.layer.close(index);
                      });
                  }
              }
            });

            return false;
          });
          

          // 获取部门数据
            $.ajax({
                type: "post",// http请求方法
                url:"/dept/list/all",// 后端的接口
                dataType: "json", // 后端返回的数据类型
                async: false, // 异步转同步
                success: function (res) {// 服务器成功响应之后对应的逻辑
                    if (res.code === 200){
                        // 清空容器
                        $("select[name=deptId]").empty();
                        $(res.data).each(function () {
                            var option = $('<option value="'+this.deptId+'">'+this.deptName+'</option>');
                            $("select[name=deptId]").append(option);
                        })
                        // 重新渲染表单
                        form.render();
                    }
                }
            });

          // 获取角色数据
            $.ajax({
                type: "post",// http请求方法
                url:"/role/list/all",// 后端的接口
                dataType: "json", // 后端返回的数据类型
                async: false, // 异步转同步
                success: function (res) {// 服务器成功响应之后对应的逻辑
                    if (res.code === 200){
                        // 清空容器
                        $("#role-container").empty();
                        $(res.data).each(function () {
                            var input = $('<input type="checkbox" name="roleIds" value="'+this.roleId+'" lay-skin="primary" title="'+this.roleName+'" >')
                            $("#role-container").append(input)
                        })
                        // 重新渲染表单
                        form.render();
                    }
                }
            });

          // 获取员工数据
            $.ajax({
                type: "post",// http请求方法
                url:"/empl/detail/data",// 后端的接口
                data:{emplId: $("input[name=emplId]").val()},
                dataType: "json", // 后端返回的数据类型
                async: false, // 异步转同步
                success: function (res) {// 服务器成功响应之后对应的逻辑
                    console.log(res);
                    if (res.code === 200){
                        var data = res.data;
                        // 做数据回显
                        $("input[name=emplCode]").val(data.emplCode);
                        $("input[name=emplName]").val(data.emplName);
                        $("input[name=emplPhone]").val(data.emplPhone);
                        $("textarea[name=emplRemark]").val(data.emplRemark);
                        // 回显性别/状态
                        $("input[name=emplGender]").each(function () {
                            if ($(this).val() == data.emplGender){
                                $(this).prop("checked", true);
                            }
                        });
                        $("input[name=emplStatus]").each(function () {
                            if ($(this).val() == data.emplStatus){
                                $(this).prop("checked", true);
                            }
                        });
                        // 回显部门数据
                        $("select[name=deptId]").val(data.deptId);
                        // 回显角色数据
                        $("input[name=roleIds]").each(function (index, input) {
                            $(data.roles).each(function (index, role) {
                                // 判断input的值和role的roleId如果相等，选中即可
                                if ($(input).val() == role.roleId){
                                    $(input).prop("checked", true);
                                }
                            })
                        });
                        form.render();
                    }
                }
            });
        });
    </script>

  </body>

</html>